<template>
    <app>
        <el-button type="success" @click="dialogFormVisible = true" plain>增加公告</el-button>
        <div>
            <el-dialog title="增加公告" :visible.sync="dialogFormVisible">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <el-form-item label="名称">

                        </el-form-item>
                        <el-input placeholder="请输入标题" v-model="addForm.title"></el-input>
                    </div>
                    <el-input
                            type="textarea"
                            :rows="10"
                            placeholder="请输入正文"
                            v-model="addForm.content">
                    </el-input>
                    <el-button type="danger" @click="dialogFormVisible = false" plain>关闭</el-button><el-button type="success" @click="addInformation" plain>保存</el-button>
                </el-card>
            </el-dialog>
        </div>
        <div  style="width: 900px">
            <el-card style="margin-top: 20px" v-for="items in selectInformation" :key="items.id">
                <div slot="header" class="clearfix">
                    <span>{{items.title}}</span> <span style="background-color: #F56C6C;color: #ffffff;" v-if="items.serialnum==1">置顶</span>
                    <el-form-item label="名称">
                    </el-form-item>
                </div>
                <el-link icon="el-icon-time" disabled>{{items.createtime}}</el-link>
                <pre>{{items.content}}</pre>

                <el-tooltip class="item" effect="dark" content="删除" placement="top-start">
                    <el-button  style="padding: 5px 5px;" @click="deleteInformationFun(items.id)" type="danger" icon="el-icon-delete" circle></el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="置顶" placement="top-start">
                <el-button style="padding: 5px 5px;" @click="topInformationFun(items.id)" type="warning" icon="el-icon-top" circle></el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="修改" placement="top-start">
                    <el-button  style="padding: 5px 5px;" @click="updateInformationFun(items.id,items.title,items.content)" type="primary" icon="el-icon-edit" circle></el-button>
                </el-tooltip>
            </el-card>
            <el-pagination
                    background
                    layout="prev, pager, next"
                    :page-count="thisPageNum"
                    :current-page="thisPageNumPage"
                    @current-change="thisPageNumChange">
            </el-pagination>
        </div>
        <div>
            <el-dialog title="修改公告" :visible.sync="updateFormVisible">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <el-form-item label="名称">

                        </el-form-item>
                        <el-input placeholder="请输入标题" v-model="updateForm.title"></el-input>
                    </div>
                    <el-input
                            type="textarea"
                            :rows="10"
                            placeholder="请输入正文"
                            v-model="updateForm.content">
                    </el-input>
                    <el-button type="danger" @click="updateFormVisible = false" plain>关闭</el-button><el-button type="success" @click="updateSaveInformationFun" plain>保存</el-button>
                </el-card>
            </el-dialog>
        </div>
    </app>
</template>

<script>
    export default {
        name: "InformationManagement",
        data(){
            return{
                dialogFormVisible: false,
                updateFormVisible:false,
                addForm:{
                    title:'',
                    content:''
                },
                selectInformation:[
                    '','','','',''
                ],
                thisPageNum:2,
                updateForm:{
                    id:0,
                    title:'',
                    content:''
                }
            }
        },
        methods:{
            //增加公告
            addInformation(){
                this.$api.addInformation(this.addForm)
                .then(res=>{
                    if (res.code==200)
                    {
                        this.dialogFormVisible=false;
                        //刷新公告
                        this.selectInformationFun(1);
                        this.$message.success('保存成功,15秒后生效')
                    }
                    else
                    {
                        this.$message.error(res.message)
                    }
                })
            },
            //查询公告
            selectInformationFun(pageNum){
                this.$api.selectInformations(pageNum)
                .then(res=>{
                    if (res.code==200){
                        this.selectInformation=res.data.data;
                        this.thisPageNum=res.data.pages
                    }
                })
            },
            //分页
            thisPageNumChange(thisPageNumPage){
                this.selectInformationFun(thisPageNumPage);
            },
            //更新公告按钮
            updateInformationFun(id,title,content){
                this.updateFormVisible=true;
                this.updateForm.id=id;
                this.updateForm.title=title;
                this.updateForm.content=content;
            },
            //保存更新公告按钮
            updateSaveInformationFun(){
                this.$api.updateInformations(this.updateForm)
                .then(res=>{
                    if (res.code==200){
                        this.updateFormVisible=false;
                        //刷新公告
                        this.selectInformationFun(1);
                        this.$message.success('保存成功,15秒后生效')
                    }
                    else {
                        this.$message.error(res.message)
                    }
                })
            },
            //删除公告
            deleteInformationFun(id){
                this.$confirm('此操作将永久删除该公告, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$api.deleteInformations(id)
                    .then(res=>{
                        if (res.code=200){
                            //刷新公告
                            this.selectInformationFun(1);
                            this.$message.success('删除成功,15秒后生效')
                        }
                        else
                        {
                            this.$message.error(res.message)
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            topInformationFun(id){
                this.$api.topInformations(id)
                .then(res=>{
                    if (res.code==200){
                        //刷新公告
                        this.selectInformationFun(1);
                        this.$message.success('置顶成功,15秒后生效')
                    }
                    else {
                        this.$message.error(res.message)
                    }
                })
            }
        },
        mounted() {
            this.selectInformationFun(1);
        }
    }
</script>

<style scoped>

</style>
